package cn.itcast.erp.service.impl;

import cn.itcast.erp.dao.mapper.EmpMapper;
import cn.itcast.erp.dao.mapper.StoreMapper;
import cn.itcast.erp.entity.Emp;
import cn.itcast.erp.entity.Goodstype;
import cn.itcast.erp.entity.Store;
import cn.itcast.erp.entity.utils.AjaxResult;
import cn.itcast.erp.service.StoreService;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/*
 * @author 刘鑫鑫
 * @date 2019/6/5
 * */
@Service("storeService")
public class StoreServiceImpl implements StoreService {

    @Resource
    private StoreMapper storeMapper;
    @Resource
    private EmpMapper empMapper;

    //添加仓库
    @Override
    public int insertStore(Store store) {
        if (store.getEname()!=null){
            Emp emp = this.empMapper.selectEmpId(store.getEname());
            store.setEmpuuid(emp.getUuid());
        }
        int i = this.storeMapper.insertStore(store);
        return i;
    }

    //修改仓库
    @Override
    public int updateStore(Store store) {
        if (store.getEname()!=null){
            Emp emp = this.empMapper.selectEmpId(store.getEname());
            store.setEmpuuid(emp.getUuid());
        }
            int i = this.storeMapper.updateStore(store);

            return i;

    }

    //查询所有
    @Override
    public AjaxResult selectCondition(Store store) {

        int total = this.storeMapper.count(store);
        List<Store> rows = this.storeMapper.selectCondition(store);

        AjaxResult ar = new AjaxResult();
        ar.setTotal(total);
        ar.setRows(rows);

        return ar;
    }



    //删除仓库
    @Override
    public int deleteStore(Long uuid) {
        int i = this.storeMapper.deleteStore(uuid);
        return i;
    }



    //查询所有仓库(不分页)
    @Override
    public List<Store> findAllStore() {
        return this.storeMapper.findAllStore ();
    }

    @Override
    public Store findStoreById(Long uuid) {
        return this.storeMapper.findStoreById (uuid);
    }

    // 导出Excel
    @Override
    public InputStream getAllStoreEx() {
        //添加数据
        List<Store> store = storeMapper.getAllStoreEx();

        HSSFWorkbook sheets = new HSSFWorkbook();  // 定义一个新的工作簿
        Sheet sheet = sheets.createSheet("仓库信息");  // 创建第一个新的Sheet页


        //创建第一行（也可以成为表头）
        Row row1 = sheet.createRow(0);
        sheet.setColumnWidth(0, 20 * 256);
        sheet.setColumnWidth(1, 30 * 256);
        sheet.setColumnWidth(2, 20 * 256);
        sheet.setColumnWidth(3, 30 * 256);



        //给表头第一行一次创建单元格
        String[] headers = {"编号","名称","地址","仓库管理员"};
        for(int i = 0; i < headers.length; i++){
            row1.createCell(i).setCellValue(headers[i]);
            row1.setHeightInPoints(30);//设置行的高度是30
        }


        //向单元格里添加数据
        for(short i=0;i<store.size();i++){
            row1 = sheet.createRow(i+1);
            row1.createCell(0).setCellValue(store.get(i).getUuid());
            row1.createCell(1).setCellValue(store.get(i).getName());
            row1.createCell(2).setCellValue(store.get(i).getAddress());
            //通过员工的uuid来查询员工的名字返回给仓库
            Emp byuuid = empMapper.findByuuid(store.get(i).getEmpuuid());
            row1.createCell(3).setCellValue(byuuid.getName());
            row1.setHeightInPoints(30);//设置行的高度是30
        }
        //字节流缓存区
        ByteArrayOutputStream bo = new ByteArrayOutputStream();
        try {
            sheets.write(bo);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new ByteArrayInputStream(bo.toByteArray());
    }




}
